/**
 * Created by Tuffy on 16/6/7.
 */
'use strict';

angular.module('sales.services', [])
    .service('HttpService', ['$http', '$q', '$state', 'Settings', '$rootScope', function ($http, $q, $state, Settings, $rootScope) {
        // 格式化getter参数
        function formatGetParams(url, params) {
            var hostName = '';
            for (var i in (params || {})) {
                var thisParams = params[i];
                if (i == 'host') {
                    hostName = thisParams;
                    continue;
                }
                if (url.indexOf('?') >= 0) {
                    url += '&' + i + '=' + thisParams;
                } else {
                    url += '?' + i + '=' + thisParams;
                }
            }
            return encodeURIComponent(url);
        };

        return {
            post: function (url, params) {
                var index = layer.load(2, {
                    shade: [0.3, '#F7F7F7'] //0.1透明度的白色背景
                });
                var deferred = $q.defer();
                return $http.post(Settings.Context.path + url, params, {
                    headers: {
                        'angular': true
                    }
                }).success(function (data, status, headers, config) {
                    deferred.resolve(data, status, headers, config);
                    layer.close(index);
                }).error(function (data, status, headers, config) {
                    deferred.reject(data, status, headers, config);
                    layer.close(index);
                });
                return deferred.promise;
            },
            get: function (url, params) {
                var index = layer.load(2, {
                    shade: [0.3, '#F7F7F7'] //0.1透明度的白色背景
                });
                var deferred = $q.defer();
                return $http.get(Settings.Context.path + formatGetParams(url, params), {
                    headers: {
                        'Content-Type': 'application/json',
                        'angular': true
                    }
                }).success(function (data, status, headers, config) {
                    deferred.resolve(data, status, headers, config);
                    layer.close(index);
                }).error(function (data, status, headers, config) {
                    deferred.reject(data, status, headers, config);
                    layer.close(index);
                })
                return deferred.promise;
            }
        };
    }])
    .service('UserTruckService', ['HttpService', function (HttpService) {
        var url = '/usertruck';
        return {
            list: function (params) {
                var action = '/list';
                return HttpService.get(url + action, params);
            },
            audit: function (truckId, driverId, status, truckType) {
                var action = '/audit';
                return HttpService.post(url + action, {
                    truckId: truckId,
                    driverId: driverId,
                    status: status,
                    truckType: truckType
                });
            },
            truckDetail: function (truckId) {
                var action = '/truckDetail/' + truckId;
                return HttpService.get(url + action);
            },
            driverDetail: function (truckId) {
                var action = '/driverDetail/' + truckId;
                return HttpService.get(url + action);
            },
            loadPic: function (id) {
                var action = '/loadpic/' + id;
                return HttpService.get(url + action);
            }
        }
    }]).service('AuditService', ['HttpService', function (HttpService) {
    var url = '/audit';
    return {
        detail: function (params) {
            var action = '/detail';
            return HttpService.post(url + action, params);
        }
    }
}]);